package ynu.edu.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ynu.edu.entity.FoodCategory;
import ynu.edu.exception.ResourceNotFoundException;
import ynu.edu.repository.FoodCategoryRepository;
import ynu.edu.service.FoodCategoryService;

import java.util.List;

/**
 * 食品分类服务实现类
 */
@Service
public class FoodCategoryServiceImpl implements FoodCategoryService {
    
    @Autowired
    private FoodCategoryRepository foodCategoryRepository;
    
    @Override
    public List<FoodCategory> getCategoriesByBusinessId(Integer businessId) {
        return foodCategoryRepository.findByBusinessId(businessId);
    }
    
    @Override
    public FoodCategory getCategoryById(Integer categoryId) {
        return foodCategoryRepository.findById(categoryId)
                .orElseThrow(() -> new ResourceNotFoundException("未找到ID为 " + categoryId + " 的食品分类"));
    }
    
    @Override
    public FoodCategory getCategoryByBusinessIdAndCategoryId(Integer businessId, Integer categoryId) {
        FoodCategory category = foodCategoryRepository.findByBusinessIdAndCategoryId(businessId, categoryId);
        if (category == null) {
            throw new ResourceNotFoundException("未找到商家ID为 " + businessId + " 且分类ID为 " + categoryId + " 的食品分类");
        }
        return category;
    }
}